/**
 * Created by gongxun on 17/3/20.
 */

var PszCardLayer = cc.Layer.extend({
    //card
    mCardLayer : null,
    mCardss : null,

    //chip
    mChipLayer : null,

    //看牌按钮
    mPszTableCardTControl : null,

    //比牌
    mPszTableBipaiLayer : null,

    //动画层
    mAnimLayer : null,

    //黑色层
    mGrayLayer : null,
    ctor : function(){
        this._super();

        //chips
        this.mChipLayer = new cc.Layer();
        this.addChild(this.mChipLayer);

        //card
        this.mCardLayer = new cc.Layer();
        this.addChild(this.mCardLayer, 1);

        //比牌
        this.mPszTableBipaiLayer = new PszTableBipaiLayer();
        this.mCardLayer.addChild(this.mPszTableBipaiLayer, 1);

        this.mCardss = [];
        for (var i = 0; i < 5; i++){
            var cards = [];
            for(var j = 0; j < 3; j++){
                var card = new PszCard();
                card.setVisible(false);
                this.mCardLayer.addChild(card, 2);
                cards.push(card);
            }
            this.mCardss.push(cards);
        }

        //看牌按钮
        this.mPszTableCardTControl = new PszTableCardTControl();
        this.addChild(this.mPszTableCardTControl, 1);

        //动画层
        this.mAnimLayer = new cc.Layer();
        this.addChild(this.mAnimLayer, 1);

        //黑色
        this.mGrayLayer = new cc.LayerColor(cc.color(0, 0, 0, 255), mo.gameSize.x, mo.gameSize.y);
        this.addChild(this.mGrayLayer, 2);
        this.mGrayLayer.setVisible(false);
    },

    reset : function(){
        //筹码
        this.mChipLayer.removeAllChildren();
        this.mChipLayer.setLocalZOrder(0);

        //看牌
        this.mPszTableCardTControl.reset();

        //动画层
        this.mAnimLayer.removeAllChildren();

        //黑色
        this.mGrayLayer.setVisible(false);
    },

    //结算变黑
    setJiesuanGray : function(){
        this.mGrayLayer.setVisible(true);
        this.mGrayLayer.setOpacity(0);
        this.mGrayLayer.stopAllActions();

        var that = this;
        this.mGrayLayer.runAction(cc.sequence(
            cc.fadeTo(0.2, 100),
            cc.delayTime(0.8),
            cc.callFunc(function(){
                that.mChipLayer.setLocalZOrder(3);
            }),
            cc.delayTime(0.8),
            cc.fadeOut(0.2),
            cc.callFunc(function(){
                that.mGrayLayer.setVisible(false);
                that.mChipLayer.setLocalZOrder(0);
            })
        ))
    },

    //返回筹码
    getChipSprite : function(){
        var chip = new PszChip();
        this.mChipLayer.addChild(chip);
        return chip;
    },

    //发牌动画
    fapai : function(seats, cardNums){
        //初试位置
        var startP = cc.p(mo.pszCardHelper.cardWellPoint.x, mo.pszCardHelper.cardWellPoint.y);
        for (var i = 0; i < 5; i++){
            var isShow = seats[i];

            for(var j = 0; j < 3; j++){
                var card = this.mCardss[i][j];
                card.stopAllActions();
                card.setVisible(isShow);

                if (isShow){
                    var cardNum = 0;
                    if (cardNums[i].length != 0){
                        cardNum = cardNums[i][j];
                    }
                    card.setCardNum(cardNum);
                    card.setCardKind(i, 1);
                    card.setPosition(startP);
                    card.setScale(mo.pszCardHelper.cardWellScale);
                    card.setRotation(0);
                    startP.y += 2;
                }
            }
        }

        var index = 0;
        for (var i = 0; i < 5; i++){
            var isShow = seats[i];
            if (isShow){
                for (var j = 0; j < 3; j++){
                    var card = this.mCardss[i][j];
                    var toPos = mo.pszCardHelper.getInhandCardPos(i, j);
                    var toScale = i == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale;
                    card.runAction(cc.sequence(
                        cc.delayTime(index * 0.2 + j * 0.2),
                        cc.callFunc(function(){
                            this.setRotation(-360);
                        }, card),
                        cc.spawn(
                            cc.moveTo(0.3, toPos),
                            cc.scaleTo(0.3, toScale),
                            cc.rotateBy(0.3, 360)
                        )
                    ));
                }
                index++;
            }
        }
    },

    //设置手牌 seats bool(len 5) cardStatuss (0:牌背 1:离桌)  myCardNums len 3 or 0
    setInhandCard : function(seats, cardStatuss, myCardNums){
        for (var i = 0; i < 5; i++){
            var isShow = seats[i];
            if (!isShow){
                //不显示的隐藏
                for (var j = 0; j < 3; j++) {
                    var card = this.mCardss[i][j];
                    card.stopAllActions();
                    card.setVisible(false);
                }
            }else{
                this.setInhandCardForSeat(i, cardStatuss[i], i == 0 ? myCardNums : []);
            }
        }
    },

    //设置一个玩家手牌状态
    setInhandCardForSeat : function(seat, cardStatus, cardNums){
        for (var i = 0; i < 3; i++){
            var card = this.mCardss[seat][i];
            card.stopAllActions();
            card.setVisible(true);
            card.setRotation(0);
            var toPos = mo.pszCardHelper.getInhandCardPos(seat, i);
            var toScale = seat == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale;
            card.setScale(toScale);
            card.setPosition(toPos);

            card.setCardNum(0);
            card.setCardKind(seat, 1);

            if (cardNums.length != 0){
                //显示牌
                card.setCardNum(cardNums[i]);
                card.setCardKind(seat, 1);
            }else if (cardStatus == 1){
                card.setCardKind(seat, 2);
            }
        }
    },

    //设置自己的手牌状态 status 0:不显示 1:闷牌 2:可以看牌 3:比牌输 4:弃牌
    setInhandCardStatus : function(status, delayTime){
        if (status == 0){
            this.mPszTableCardTControl.closeTishi();
        }else if (status == 1){
            this.mPszTableCardTControl.showMen(delayTime);
        }else if (status == 2){
            this.mPszTableCardTControl.showSee(delayTime);
        }else if (status == 3){
            this.mPszTableCardTControl.showBipaiShu(delayTime);
        }else if (status == 4){
            this.mPszTableCardTControl.showQipai(delayTime);
        }
    },

    //设置自己的手牌牌型
    setInhandCardType : function(cardType, delayTime){
        this.mPszTableCardTControl.showCardType(cardType, delayTime);
    },

    //下注动画
    xiazhu : function(seat, chipNum, delyTime){
        var time = 0;
        if (delyTime != undefined){
            time = delyTime;
        }

        var chip = this.getChipSprite();
        chip.setPosition(mo.pszCardHelper.seats[seat]);
        chip.setChipNum(chipNum);
        chip.setVisible(false);

        var toPoint = mo.pszCardHelper.getRandomChipPoint();
        chip.runAction(cc.sequence(
            cc.delayTime(time),
            cc.show(),
            cc.moveTo(0.2, toPoint)
        ))
    },

    //赢了飞筹码
    setWinChips : function(winSeat){
        var toPos = mo.pszCardHelper.seats[winSeat];
        var chips = this.mChipLayer.getChildren();

        this.scheduleOnce(function(dt){
            for (var i = 0; i < chips.length; i++){
                var chip = chips[i];
                chip.stopAllActions();
                chip.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveBy(0.5, cc.p(0, 10)),
                        cc.scaleTo(0.5, 0.9)
                    ),
                    cc.callFunc(function () {
                        //音效
                        mo.soundHelper.playEffectWinCard();
                    }),
                    cc.spawn(
                        cc.moveTo(0.4, toPos),
                        cc.scaleTo(0.4, 0.8)
                    ),
                    cc.delayTime(0.4),
                    cc.removeSelf()
                ));
            }
        }, 1);
    },

    //桌面筹码
    setTableChips : function(chipAllNums){
        //计算拍桌筹码
        var chipNums = [];
        var leftChip = chipAllNums;
        var chipTemps = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        //先添加每个人开始扔下去的筹码
        for(var i = 0; i < mo.pszTableStatus.getPlayerCount(); i++){
            leftChip -= 1;
            chipNums.push(1);
        }

        //从大到小找筹码
        for (var i = 9; i >= 0; i--){
            var temp = chipTemps[i];
            var count = parseInt(leftChip / temp);
            for (var j = 0; j < count; j++){
                chipNums.push(temp);
            }
            leftChip = leftChip - count * temp;

            if (leftChip <= 0){
                break;
            }
        }

        //显示出来
        for (var i = 0; i < chipNums.length; i++){
            var chip = this.getChipSprite();
            chip.setPosition(mo.pszCardHelper.getRandomChipPoint());
            chip.setChipNum(chipNums[i]);
        }
    },

    //看牌
    kanpai : function(cardNums){
        //牌动画
        this.fanpai(0, cardNums);
    },

    //翻牌动画
    fanpai : function(seat, cardNums){
        //牌动画
        for (var i = 0; i < 3; i++){
            var card = this.mCardss[seat][i];
            card.setTag(cardNums[i]);
            card.stopAllActions();
            card.setPosition(mo.pszCardHelper.getInhandCardPos(seat, i));
            card.setScale(seat == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale);

            card.runAction(cc.sequence(
                cc.moveTo(0.3, mo.pszCardHelper.getInhandCardPos(seat, 0)),
                cc.rotateTo(0.2, 0, 90),
                cc.callFunc(function(){
                    this.setCardNum(this.getTag());
                    this.setCardKind(seat, 1);
                    this.setRotationX(0);
                    this.setRotationY(-90);
                }, card),
                cc.rotateTo(0.2, 0, 0),
                cc.moveTo(0.3, mo.pszCardHelper.getInhandCardPos(seat, i))
            ));
        }
    },

    //开始比牌 能否比牌
    setStartBipai : function(compares){
        this.mPszTableBipaiLayer.setStartBipai(compares);

        //如果只有一个比牌对象就自动发送比牌消息
        var count = 0;
        var seat = 0;
        for (var i = 0; i < compares.length; i++){
            if (compares[i] == true){
                seat = i;
                count++;
            }
        }
        if (count == 1){
            mo.pszTableStatus.sendBipaiAction(seat);
        }
    },

    //取消比牌
    setEndBipai : function(){
        this.mPszTableBipaiLayer.setEndBipai();
    },

    //比牌
    bipai : function(seat0, seat1, winSeat, cardNumss0, cardNumss1){
        //隐藏看牌控件
        this.mPszTableCardTControl.stopAllActions();
        this.mPszTableCardTControl.setVisible(false);
        this.mPszTableCardTControl.runAction(cc.sequence(
            cc.delayTime(3.5),
            cc.show()
        ));

        //比牌ui动画
        this.mPszTableBipaiLayer.bipai(seat0, seat1, winSeat);

        //比牌牌动画
        for (var i = 0; i < 5; i++){
            var zorder = (i == seat0 || i == seat1) ? 2 : 0;
            this.mCardss[i][0].setLocalZOrder(zorder);
            this.mCardss[i][1].setLocalZOrder(zorder);
            this.mCardss[i][2].setLocalZOrder(zorder);

            var scale = 1.28;
            var toPos0 = cc.p(mo.gameSize.x / 2 - 300, mo.gameSize.y / 2);
            var toPos1 = cc.p(mo.gameSize.x / 2 + 300, mo.gameSize.y / 2);

            if (seat0 == i){
                for(var j = 0; j < 3; j++){
                    var card = this.mCardss[i][j];
                    card.stopAllActions();
                    card.setPosition(mo.pszCardHelper.getInhandCardPos(i, j));
                    card.setScale(i == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale);
                    card.setTag(j);
                    card.runAction(cc.sequence(
                        cc.moveTo(0.2, mo.pszCardHelper.getInhandCardPos(i, 0)),
                        cc.spawn(
                            cc.moveTo(0.4, toPos0),
                            cc.scaleTo(0.4, scale)
                        ),
                        cc.delayTime(1.2),
                        cc.callFunc(function(){
                            if (winSeat != seat0){
                                this.setCardKind(seat0, 2);
                            }
                        }, card),
                        cc.delayTime(1.5),
                        cc.spawn(
                            cc.moveTo(0.4, mo.pszCardHelper.getInhandCardPos(i, 0)),
                            cc.scaleTo(0.4, i == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale)
                        ),
                        cc.callFunc(function(){
                            if (cardNumss0.length != 0 && winSeat != seat0){
                                var cardNum = cardNumss0[this.getTag()];
                                this.setCardNum(cardNum);
                                this.setCardKind(seat0, 2);
                            }
                        }, card),
                        cc.moveTo(0.2, mo.pszCardHelper.getInhandCardPos(i, j))
                    ));
                }

                //show 显示1.8s 结束1.1s
                var waitSprite = new cc.Sprite();
                waitSprite.setSpriteFrame("psz_bg3.png");
                waitSprite.setScale(scale);
                waitSprite.setPosition(toPos0);
                waitSprite.setOpacity(0);
                this.mAnimLayer.addChild(waitSprite);
                waitSprite.setVisible(false);
                waitSprite.runAction(cc.sequence(
                    cc.delayTime(1.5),
                    cc.show(),
                    cc.fadeIn(0.3),
                    cc.delayTime(0.8),
                    cc.fadeOut(0.3),
                    cc.removeSelf()
                ));
            }
            if (seat1 == i){
                for(var j = 0; j < 3; j++){
                    var card = this.mCardss[i][j];
                    card.stopAllActions();
                    card.setPosition(mo.pszCardHelper.getInhandCardPos(i, j));
                    card.setScale(i == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale);
                    card.setTag(j);
                    card.runAction(cc.sequence(
                        cc.moveTo(0.2, mo.pszCardHelper.getInhandCardPos(i, 0)),
                        cc.spawn(
                            cc.moveTo(0.4, toPos1),
                            cc.scaleTo(0.4, scale)
                        ),
                        cc.delayTime(1.2),
                        cc.callFunc(function(){
                            if (winSeat != seat1){
                                this.setCardKind(seat1, 2);
                            }
                        }, card),
                        cc.delayTime(1.5),
                        cc.spawn(
                            cc.moveTo(0.4, mo.pszCardHelper.getInhandCardPos(i, 0)),
                            cc.scaleTo(0.4, i == 0 ? mo.pszCardHelper.inhandMyScale : mo.pszCardHelper.inhandScale)
                        ),
                        cc.callFunc(function(){
                            if (cardNumss1.length != 0 && winSeat != seat1){
                                var cardNum = cardNumss1[this.getTag()];
                                this.setCardNum(cardNum);
                                this.setCardKind(seat1, 2);
                            }
                        }, card),
                        cc.moveTo(0.2, mo.pszCardHelper.getInhandCardPos(i, j))
                    ));
                }

                var waitSprite = new cc.Sprite();
                waitSprite.setSpriteFrame("psz_bg3.png");
                waitSprite.setScale(scale);
                waitSprite.setPosition(toPos1);
                waitSprite.setOpacity(0);
                this.mAnimLayer.addChild(waitSprite);
                waitSprite.setVisible(false);
                waitSprite.runAction(cc.sequence(
                    cc.delayTime(1.5),
                    cc.show(),
                    cc.fadeIn(0.3),
                    cc.delayTime(0.8),
                    cc.fadeOut(0.3),
                    cc.removeSelf()
                ));
            }
        }
    }
});